package com.sixmac.platform;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.ClientClassChapter;
import com.sixmac.service.ClientClassChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClientClassChapterController
 * 2018-07-17 10:05:28
 *
 * @author Blade
 */
@Controller
@RequestMapping("/taskClass")
public class TaskClassController extends BaseController {
    private static String CODE = "taskClass";
    private static String PREFIX = "t_class_chapter";
    private static String LIST_SOURCE = "taskClass.list";
    private static String BASE_PATH = "/platform/taskClass/";

    @Autowired
    ClientClassChapterService service;

    @RequestMapping("courseTreeList")
    @ResponseBody
    public AjaxResult courseTreeList() {
        String sql = "select id,name, pid as 'pId','false' as 'open' from t_class_chapter ";
        List<Map> mapList = Db.selectList(sql);
        return json(mapList);
    }

    @RequestMapping("getPidById")
    @ResponseBody
    public AjaxResult getPidById(Integer id) {
        String sql = "select * from t_class_chapter where pid = " + id;
        List<Map> list = Db.selectList(sql);
        if (!list.isEmpty()) {
            return success("");
        }
        return error("");
    }

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clientClassChapter.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clientClassChapter_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        ClientClassChapter clientClassChapter = service.findById(id);
        mm.put("model", JsonKit.toJson(clientClassChapter));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "clientClassChapter_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        ClientClassChapter clientClassChapter = service.findById(id);
        mm.put("model", JsonKit.toJson(clientClassChapter));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "clientClassChapter_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        ClientClassChapter clientClassChapter = mapping(PREFIX, ClientClassChapter.class);
        clientClassChapter.setCreatetime(new Date());
        boolean temp = service.save(clientClassChapter);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        ClientClassChapter clientClassChapter = mapping(PREFIX, ClientClassChapter.class);
        boolean temp = service.update(clientClassChapter);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        String[] id = Convert.toStrArray(ids);
        for (String i : id) {
            Blade.create(ClientClassChapter.class).deleteBy("pid = " + i, null);
        }
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
